//#include "stdafx.h"
#include "base_common_system.h"
#include <shlobj.h>
#define MAX_PATH_LEN   1024

string_t _base_get_system_dir()
{
	char_t * pBuf = new char_t[MAX_PATH_LEN];
	unsigned int nBufLen = MAX_PATH_LEN;
	unsigned int nLen = ::GetSystemDirectory( pBuf, nBufLen );
	if( nLen > nBufLen )
	{
		if( pBuf != NULL )
		{
			delete []pBuf;
		}
		pBuf = new char_t[nLen+1];
		nBufLen = nLen+1;
		nLen = ::GetSystemDirectory( pBuf, nBufLen );
	}
	string_t strDir;
	if( nLen > 0 )
	{
		strDir = pBuf;
		delete []pBuf;
	}
	else
	{
		strDir = _T("");
	}
	return strDir;
}
string_t _base_get_window_dir()
{
	char_t * pBuffer = new char_t[MAX_PATH_LEN];
	unsigned int nBufLen = MAX_PATH_LEN;
	unsigned int nLen = ::GetWindowsDirectory( pBuffer, nBufLen );
	if( nLen > nBufLen )
	{
		if( pBuffer != NULL )
		{
			delete []pBuffer;
		}
		pBuffer = new char_t[nLen+1];
		nBufLen = nLen+1;
		nLen = ::GetWindowsDirectory( pBuffer, nBufLen );
	}
	string_t strDir;
	if( nLen > 0 )
	{
		strDir = pBuffer;
		delete []pBuffer;
	}
	else
	{
		strDir = _T("");
	}
	return strDir;
}
string_t _base_get_inf_dir()
{
	string_t strWinDir = _base_get_window_dir();
	if( strWinDir != _T("") )
	{
		return (strWinDir+_T("\\inf"));
	}
	else
	{
		return _T("");
	}
}
string_t _base_get_driver_dir()
{
	string_t strWinDir = _base_get_window_dir();
	if( strWinDir != _T("") )
	{
		return (strWinDir+_T("\\driver"));
	}
	else
	{
		return _T("");
	}
}
string_t _base_get_help_dir()
{
	string_t strWinDir = _base_get_window_dir();
	if( strWinDir != _T("") )
	{
		return (strWinDir+_T("\\help"));
	}
	else
	{
		return _T("");
	}
}

string_t _base_get_app_path()
{
	int nBufLen = MAX_PATH_LEN;
	char_t * pBuf = new char_t[MAX_PATH_LEN];
	int nLen = GetModuleFileName( NULL, pBuf, nBufLen );
	if( nLen > nBufLen )
	{
		delete []pBuf;
		pBuf = new char_t[nLen+1];
		nLen = GetModuleFileName( NULL, pBuf, nBufLen );
		if( nLen == 0 )
		{
			delete []pBuf;
			return _T("");
		}
	}
	if( nLen == 0 )
	{
		delete []pBuf;
		return _T("");
	}
	string_t strPath = pBuf;
	int pos = strPath.rfind( _T("\\") );
	if( pos > 0 )
		strPath = strPath.substr( 0, pos );
	delete []pBuf;
	return strPath;
}
string_t _base_get_current_dir()
{
	int nBufLen = MAX_PATH_LEN;
	char_t * pBuf = new char_t[MAX_PATH_LEN];
	int nLen = GetCurrentDirectory( nBufLen, pBuf );
	if( nLen > nBufLen )
	{
		delete []pBuf;
		pBuf = new char_t[nLen+1];
		nLen = GetCurrentDirectory( nLen, pBuf );
		if( nLen == 0 )
		{
			delete []pBuf;
			return _T("");
		}
	}
	if( nLen == 0 )
	{
		delete []pBuf;
		return _T("");
	}
	string_t strDir = pBuf;
	delete []pBuf;

	return strDir;
}
bool _base_set_current_dir( cstr_t lpszPath )
{
	return ( ::SetCurrentDirectory( lpszPath ) == TRUE );
}

typedef HRESULT (WINAPI *pfnSHGetFolderPath)( HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath );
string_t _base_get_special_dir( int nFolder )
{
	string_t strDir = _T("");
	HMODULE hDll = ::LoadLibrary( _T("shell32.dll") );
	if( hDll == NULL )
		return strDir;
#if defined(_UNICODE)
	pfnSHGetFolderPath pfn = (pfnSHGetFolderPath)::GetProcAddress( hDll, "SHGetFolderPathW" );
#else
	pfnSHGetFolderPath pfn = (pfnSHGetFolderPath)::GetProcAddress( hDll, "SHGetFolderPathA" );
#endif
	if( pfn != NULL )
	{
		char_t path[MAX_PATH];
		if( SUCCEEDED( pfn( NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL, 0, path ) ) ) 
		{
			strDir = path;
		}
	}
	::FreeLibrary( hDll );
	return strDir;
}
string_t _base_get_system_temp_path()
{
	char_t * pBuffer = new char_t[MAX_PATH_LEN];
	unsigned int nBufLen = MAX_PATH_LEN;
	unsigned int nLen = ::GetTempPath( nBufLen, pBuffer );
	if( nLen > nBufLen )
	{
		if( pBuffer != NULL )
		{
			delete []pBuffer;
		}
		pBuffer = new char_t[nLen+1];
		nBufLen = nLen+1;
		nLen = ::GetTempPath( nBufLen, pBuffer );
	}
	string_t strDir;
	if( nLen > 0 )
	{
		strDir = pBuffer;
		delete []pBuffer;
	}
	else
	{
		strDir = _T("");
	}
	return strDir;
}